<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Paginator;

/**
 * 产品评论管理
 */
class Review extends Controller
{
    public $user_id = 0;
    public $user = array();
    public $check_date = 0;

    public function _initialize() {      
        parent::_initialize();
        if(session('?user'))
        {
            $user = session('user');
            $user = Db::table('users')->where("id", $user['id'])->find();
            session('user',$user);            
            $this->user = $user;
            $this->user_id = $user['id'];
            $this->assign('user',$user); //存储用户信息
            $this->assign('user_id',$this->user_id);
        }
    }
    
    /**
     * 2018-4-16  日期格式
     * 曹振威 开发人
     * 获取评论的子集
     * @param type $review_id
     * @param type $where
     * @return type
     */
    public function get_review($review_id,$where,$field){
        $child_review = Db::table('review')->where('superior_id',$review_id)->field($field)->paginate();
        $arr = array();
        foreach ($child_review as $key => $val ) {
            $child = $this->get_review($val["user_id"],$where,$field);
            if($child){                
                $val['child_review'] = $child;
            }
            $arr[] = $val; 
        }
        return $arr;
    }

    /**
     * 2018-4-16 日期格式
     * 曹振威 开发人
     * 评论列表
     */
    public function review_list() {
       
        $user_id = $this->user_id;
        $user_id = 1;
        $system_id = input('system_id');
        $business_id = Db::table('business')->where('business_id',$user_id)->field('id')->find();
        $key_word = input('key_word');
        if(!$system_id || !$business_id){
            $date = array('code'=>20021,'msg'=>'非法操作','data'=>'');
            exit(json_encode($date));
        }
        if($key_word){
            $where['commentary'] = ['LIKE',$key_word];
        }
        $where['business_id'] = $business_id;
        $where['system_id'] = $system_id;
        $field = 'id,system_id,user_id,user_name,commentary,createtime,review_level,review_img,order_sn,createtime,superior_id';
        $list = $this->get_review(0,$where,$field);
        if(!$list){
            $date = array('code'=>20032,'msg'=>'当前页没有数据','data'=>'');
        }else{
            $date = array('code'=>666,'msg'=>'评论获取成功','data'=>$list);
        }
        return $date;
    }
    
    /**
     * 2018-4-13 日期格式
     * 曹振威 开发人
     * 评论详情
     * @return type
     */
    public function review_info() {
        $product = array();
        $user_id = $this->user_id;
        $user_id = 1;
        $system_id = input('system_id');
        $business_id = Db::table('business')->where('business_id',$user_id)->field('id')->find();
        $review_id = input('post.id');
        if(!$system_id || !$business_id || !$review_id){
            $date = array('code'=>20021,'msg'=>'非法操作','data'=>'');
            exit(json_encode($date));
        }
        $where['business_id'] = $business_id;
        $where['system_id'] = $system_id;
        $field = 'id,system_id,user_id,user_name,commentary,createtime,review_level,review_img,order_sn,createtime,superior_id';
        $list = $this->get_review(0,$where,$field);
        foreach ($list as $key => $value) {
            $order_sn = $value['order_sn'];
            $fileds = 'id,system_id,business_id,batchcode,pay_batchode,product_id,price,quantity,createtime';
            $list['product'][] = Db::table('orders_info')->where('batchcode',$order_sn)->field($fileds)->select();
        }
        if($list){
            $date = array('code'=>666,'msg'=>'详情获取成功','data'=>$list);
        } else {
            $date = array('code'=>20033,'msg'=>'详情获取失败','data'=>'');
        }
        
        return $date;
    }
    
}
